<template>
  <el-dialog
    center
    v-if="isShow"
    v-dialogDrag
    :title="title"
    v-loading="loading"
    :width="dialogWidth"
    :before-close="close"
    :visible.sync="isShow"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-position="right"
      class="overflow pl15 pr15"
    >
      <el-row type="flex" justify="space-around">
        <el-col :span="24" class="mb15">
          <el-form-item
            label="上传图标："
            :label-width="labelWidth"
            class="required"
          >
            <Thumbnail
              @deleteUserAvatar="handleDeleteUserAvatar"
              :photos="photoArr"
            ></Thumbnail>
            <ImageUpload
              :pegs="pegs"
              :limit="limit"
              :imageMaxSize="imageMaxSize"
              @uploadFiles="handleUploadFiles"
            ></ImageUpload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="space-around">
        <el-col :span="24" class="mb15 form-inp">
          <el-form-item
            label="服务名称："
            prop="name"
            :label-width="labelWidth"
            class="required"
          >
            <el-input
              class="floor"
              v-model="form.name"
              autocomplete="off"
              placeholder="请输入服务名称"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" justify="space-around" class="mb15">
        <el-col :span="24">
          <el-form-item
            prop="title"
            label="弹窗标题："
            :label-width="labelWidth"
            class="form-inp required"
          >
            <el-input
              v-model="form.title"
              autocomplete="off"
              placeholder="请输入弹窗标题"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer align-center tiling inline-block">
      <el-button @click="close">取 消</el-button>
      <el-button class="theme-btn" @click="handleConfirm('form')"
        >保 存</el-button
      >
    </span>
    <PhotoDialog
      @showPic="handleShowPic"
      :showPic="showPic"
      :isShowPic="isShowPic"
    ></PhotoDialog>
  </el-dialog>
</template>

<script>
import "@/utils/drag.js";
import Thumbnail from "@/components/thumbnail";
import { vlength } from "@/utils/verification.js";
import PhotoDialog from "@/components/photo_dialog";
import ImageUpload from "@/components/image_upload";
import {
  addConvenientInfo,
  editConvenientInfo,
  getConvenientInfo
} from "@/apis/merchant";

export default {
  name: "Convenient_info_dialog",
  data() {
    return {
      form: {
        name: "",
        title: ""
      },
      pegs: [],
      limit: 1,
      showPic: "",
      photoArr: [],
      imageMaxSize: 1024,
      uploadFileList: [],
      isShowPic: false,
      labelWidth: "125px",
      dialogWidth: "490px",
      rules: {
        name: {
          validator: vlength(2, 20, "请输入服务名称"),
          trigger: "blur"
        },
        title: {
          validator: vlength(2, 20, "请输入弹窗标题"),
          trigger: "blur"
        }
      }
    };
  },
  computed: {
    isShow() {
      return this.addConvenientInfoDialog || this.editConvenientInfoDialog;
    }
  },
  watch: {
    editConvenientInfoDialog() {
      this.editConvenientInfoDialog && this.getConvenientInfo();
    },
    addConvenientInfoDialog() {
      this.addConvenientInfoDialog && this.resetForm();
    }
  },
  methods: {
    handleUploadFiles(props) {
      this.uploadFileList = props;
    },
    handleShowPic(data) {
      this.showPic = data;
      this.isShowPic = !this.isShowPic;
    },
    async getConvenientInfo() {
      const { object } = await getConvenientInfo({
        convenientInfoId: this.convenientInfoId
      });
      this.photoArr = object.photo.split(",");
      this.pegs = JSON.parse(JSON.stringify(this.photoArr));
      this.form = object;
    },
    handleConfirm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.editConvenientInfoDialog
            ? this.editConvenientInfo()
            : this.addConvenientInfo();
        }
      });
    },
    handleDeleteUserAvatar(delUrl) {
      this.photoArr.forEach((item, index) => {
        if (item === delUrl) {
          this.photoArr[index] = `D${item}`;
          this.pegs = this.pegs.filter(item => item !== delUrl);
        }
      });
    },
    addConvenientInfo() {
      const formData = new FormData();
      const file = this.uploadFileList[0];

      if (!this.uploadFileList.length) {
        return this.$notify.error({
          title: "错误",
          message: "请上传图标！"
        });
      }

      if (file) {
        formData.append("file", file.raw);
      }

      formData.append("name", this.form.name);
      formData.append("title", this.form.title);

      this.$store.dispatch("load/setLoading", true);
      // addConvenientInfo(formData).then(res => {
      //   if (res && res.code === "200") {
      //     this.$emit("update");
      //     this.$emit("close");
      //     this.$notify({
      //       title: res.msg,
      //       type: "success"
      //     });
      //   }
      // });
      this.$emit("update");
      this.$emit("close");
      this.$notify({
        title: "操作成功！",
        type: "success"
      });
    },
    editConvenientInfo() {
      const formData = new FormData();
      const file = this.uploadFileList[0];

      if (!this.uploadFileList.length && !this.pegs.length) {
        return this.$notify.error({
          title: "错误",
          message: "请上传图标！"
        });
      }

      if (file) {
        formData.append("file", file.raw);
      }

      formData.append("id", this.form.id);
      formData.append("name", this.form.name);
      formData.append("title", this.form.title);
      formData.append("photo", this.photoArr.join(","));
 
      this.$store.dispatch("load/setLoading", true);
      // editConvenientInfo(formData).then(res => {
      //   if (res && res.code === "200") {
      //     this.$emit("update");
      //     this.$emit("close");
      //     this.$notify({
      //       title: res.msg,
      //       type: "success"
      //     });
      //   }
      // });
      this.$emit("update");
      this.$emit("close");
      this.$notify({
        title: "操作成功！",
        type: "success"
      });
    },
    close() {
      this.$emit("close");
      this.resetForm();
    },
    resetForm() {
      this.form = {
        name: "",
        title: ""
      };
      this.photoArr = [];
      this.pegs.length = [];
      this.uploadFileList = [];
    }
  },
  components: {
    Thumbnail,
    PhotoDialog,
    ImageUpload
  },
  props: [
    "title",
    "loading",
    "convenientInfoId",
    "addConvenientInfoDialog",
    "editConvenientInfoDialog"
  ]
};
</script>

<style scoped></style>
